package com.jiukuaidao.zhulin.module.home.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.library.util.LogUtil;
import com.jiukuaidao.zhulin.api.ApiGetHomeAds;
import com.jiukuaidao.zhulin.api.ApiGetHomePros;
import com.jiukuaidao.zhulin.entity.ApiHomeAds;
import com.jiukuaidao.zhulin.entity.ApiHomePros;
import com.jiukuaidao.zhulin.entity.EventShoppingCartInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.home.contract.HomeContract;
import com.jiukuaidao.zhulin.util.ShopInfoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : H_Joker
 * Date : 2016/5/27
 * Description :
 */
public class HomePresenter extends BasePresenter<HomeContract.View, HomeContract.Model> implements HomeContract.Presenter {

    @Override
    public void setCurPageIndex(int curPageIndex) {
        mRepository.setCurPageIndex(curPageIndex);
    }

    @Override
    public int getCurPageIndex() {
        return mRepository.getCurPageIndex();
    }

    @Override
    public List<ApiHomePros.ApiHomeProItem> getProList() {
        return mRepository.getProList();
    }

    @Override
    public List<ApiHomeAds.ApiHomeActItem> getActList() {
        return mRepository.getActList();
    }

    @Override
    public List getBannerList() {
        return mRepository.getBannerList();
    }

    @Override
    public void setShopInfo(ApiHomePros.ApiHomeShop shop) {
        mRepository.setShopInfo(shop);
    }

    @Override
    public ApiHomePros.ApiHomeShop getShopInfo() {
        return mRepository.getShopInfo();
    }

    public HomePresenter(HomeContract.Model repository, HomeContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {

    }

    @Override
    public List<EventShoppingCartInfo> getShoppingCart() {
        return mRepository.getShoppingCart();
    }

    @Override
    public void setShoppingCart(List<EventShoppingCartInfo> list) {
        mRepository.setShoppingCart(list);
    }

    @Override
    public void loadPros(final int act, final int pageIndex, String keyWord, String cityId, String longitude, String latitude) {
        mRepository.loadData(new ApiGetHomePros<>(pageIndex, keyWord, longitude, latitude, cityId, new MyRetroCallBack<ApiHomePros>() {
            @Override
            public void onSuccess(ApiHomePros result, String errorCode, String msg) {
                if (result != null) {
                    List<ApiHomePros.ApiHomeProItem> goodsList = result.goods.goodsList;
                    if (mRepository.getProList() == null) {
                        mRepository.setProList(new ArrayList<ApiHomePros.ApiHomeProItem>());
                    }

                    setCurPageIndex(pageIndex);

                    ApiHomePros.ApiHomeShop shopInfo = result.shop;

                    mRepository.setShopInfo(shopInfo);

                    ShopInfoUtil.clearShopInfo();
                    ShopInfoUtil.setShopInfo(shopInfo);

                    mView.refreshShopInfoUI(mRepository.getShopInfo());

                    if (act == HomeContract.REFRESH) {
                        mRepository.getProList().clear();
                    }
                    if (goodsList != null) {
                        mRepository.getProList().addAll(goodsList);
                    }
                    /**
                     * list 无数据 也需要刷新一下RecyclerView 不然会闪退 是由于vierpager中fragment的生命周期造成的
                     */
//                    if (mRepository.getProList() != null && !mRepository.getProList().isEmpty()) {
                    if (mRepository.getProList() != null) {
                        // 刷新列表
                        mView.refreshProsUi(mRepository.getProList());
                        if (result.goods.pageIndex >= result.goods.totalPage) {
                            mView.loadComplete(act, true, false);
                        } else {
                            mView.loadComplete(act, false, false);
                        }
                    } else {
                        // 显示空列表界面
                        mView.loadComplete(act, false, true);
                    }
                }
            }

            @Override
            public void onFail(ApiHomePros result, String errorCode, String msg) {
                if (mRepository.getProList() != null && !mRepository.getProList().isEmpty()) {
                    mView.loadComplete(act, false, false);
                } else {
                    mView.loadComplete(act, false, true);
                }
                LogUtil.log(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                if (mRepository.getProList() != null && !mRepository.getProList().isEmpty()) {
                    mView.loadComplete(act, false, false);
                } else {
                    mView.loadComplete(act, false, true);
                }
            }
        }));
    }

    @Override
    public void refreshAdsAndAct(String cityId) {
        mRepository.loadData(new ApiGetHomeAds<>(String.valueOf(cityId), new MyRetroCallBack<ApiHomeAds>() {
            @Override
            public void onSuccess(ApiHomeAds result, String errorCode, String msg) {
                if (result != null) {
                    setBanner(result.bannerList);
                    setActs(result.actList);
                }
            }

            @Override
            public void onFail(ApiHomeAds result, String errorCode, String msg) {

            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {

            }
        }));
    }

    private void setBanner(List<ApiHomeAds.ApiHomeAdItem> bannerList) {
        if (bannerList == null || bannerList.isEmpty()) {
            mView.refreshBannerUi(null, null);
        } else {
            List<String> mBannerPics = new ArrayList<>();
            for (ApiHomeAds.ApiHomeAdItem actItem : bannerList) {
                mBannerPics.add(actItem.pictureUri);
            }
            mView.refreshBannerUi(mBannerPics, bannerList);
        }
    }

    private void setActs(List<ApiHomeAds.ApiHomeActItem> actList) {
        mView.refreshActsUi(actList);
    }
}
